package cn.iocoder.springboot.lab03.kafkademo.entity.Bo;

import java.lang.reflect.Field;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2023/12/27 22:44
 */
public class SubStudents extends Student {
    /**
     * 子类新增的属性
     */
    private String sex;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {


        SubStudents subStudent = new SubStudents();
        subStudent.setSex("可以设置");
        subStudent.setName("可以设置");

        Student student = new Student();
        student.setName("可以设置");

        //使用反射骚操作就能解决此问题

        int newAge = 666; //准备给age赋值的变量
        Class<? extends Student> class1 = student.getClass();
        Field declaredField = class1.getDeclaredField("age");
        declaredField.setAccessible(true);
        declaredField.set(student, newAge);

        System.out.println("打印反射赋值后的值：" + student.getAge());
    }
}